part of '../repository.dart';

const _kOneTimePurchasedItemInfoTypeValue = 'one_time_purchased_item';

class _OneTimePurchasedItemInfoDto extends Equatable implements _PurchasedItemInfoDto {
  const _OneTimePurchasedItemInfoDto._internal({
    required this.purchaseItemSku,
    required this.purchaseToken,
    required this.googlePlayUserId,
    required this.test,
    required this.acknowledgementStatusDto,
  });

  @override
  final String purchaseItemSku;

  @override
  final String purchaseToken;

  @override
  final String googlePlayUserId;

  @override
  final bool test;

  @override
  final _PurchasedItemAcknowledgementStatusDto acknowledgementStatusDto;

  @override
  String get type => _kOneTimePurchasedItemInfoTypeValue;

  @override
  List<Object?> get props {
    return [
      purchaseItemSku,
      purchaseToken,
      googlePlayUserId,
      test,
      acknowledgementStatusDto,
    ];
  }

  factory _OneTimePurchasedItemInfoDto.fromJson(Map<String, dynamic> json) {
    final acknowledgementStatusString = json[_kPurchasedItemInfoAcknowledgementStatusKey] as String;
    final acknowledgementStatusDto =
        _PurchasedItemAcknowledgementStatusDto.fromString(acknowledgementStatusString);

    return _OneTimePurchasedItemInfoDto._internal(
      purchaseItemSku: json[_kPurchasedItemInfoPurchaseItemSkuKey],
      purchaseToken: json[_kPurchasedItemInfoPurchaseTokenKey],
      googlePlayUserId: json[_kPurchasedItemInfoGooglePlayUserIdKey],
      test: json[_kPurchasedItemInfoTestKey],
      acknowledgementStatusDto: acknowledgementStatusDto,
    );
  }

  factory _OneTimePurchasedItemInfoDto.fromEntity(
      GooglePlayOneTimePurchasedItemInfo oneTimePurchasedItemInfo) {
    final acknowledgementStatusDto = _PurchasedItemAcknowledgementStatusDto.fromEntity(
        oneTimePurchasedItemInfo.acknowledgementStatus);

    return _OneTimePurchasedItemInfoDto._internal(
      purchaseItemSku: oneTimePurchasedItemInfo.purchaseItemSku,
      purchaseToken: oneTimePurchasedItemInfo.purchaseToken,
      googlePlayUserId: oneTimePurchasedItemInfo.googlePlayUserId,
      test: oneTimePurchasedItemInfo.test,
      acknowledgementStatusDto: acknowledgementStatusDto,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      _kPurchasedItemInfoTypeKey: type,
      _kPurchasedItemInfoPurchaseItemSkuKey: purchaseItemSku,
      _kPurchasedItemInfoPurchaseTokenKey: purchaseToken,
      _kPurchasedItemInfoGooglePlayUserIdKey: googlePlayUserId,
      _kPurchasedItemInfoTestKey: test,
      _kPurchasedItemInfoAcknowledgementStatusKey: acknowledgementStatusDto.toString(),
    };
  }

  @override
  GooglePlayOneTimePurchasedItemInfo toEntity() {
    return GooglePlayOneTimePurchasedItemInfo(
      purchaseItemSku: purchaseItemSku,
      purchaseToken: purchaseToken,
      googlePlayUserId: googlePlayUserId,
      test: test,
      acknowledgementStatus: acknowledgementStatusDto.toEntity(),
    );
  }
}
